package yl.hs.bmipfull.service.material.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.material.IRawMaterialInfoDao;
import yl.hs.bmipfull.daos.material.IRawMaterialInventoryDao;
import yl.hs.bmipfull.daos.material.IWarehouseDao;
import yl.hs.bmipfull.pojo.material.RawMaterialInventoryEntity;
import yl.hs.bmipfull.service.material.IRawMaterialInventoryService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  原材库存信息表服务层接口实现<br/>
 * 创建时间: 2021年05月19日 09时47分31秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class RawMaterialInventoryServiceImpl extends PurchaseBaseService implements IRawMaterialInventoryService {

    @Autowired
    IRawMaterialInventoryDao dao;
    @Autowired
    IRawMaterialInfoDao rawMaterialInfoDao;
    @Autowired
    IWarehouseDao warehouseDao;

    @Override
    public List<Map<String, Object>> queryList(PageQuery pageQuery) throws Exception {
        List<Map<String, Object>> listMap = new ArrayList<>();
        pageQuery.checkSortSides(RawMaterialInventoryEntity.class);
        var list = dao.queryList(pageQuery.getQuery());
        for (var info : list) {
            var map = info.toMap();
            listMap.add(map);
        }
        return listMap;
    }

    @Override
    public PageResult<RawMaterialInventoryEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        List<Map<String, Object>> listMap = new ArrayList<>();
        pageQuery.checkSortSides(RawMaterialInventoryEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<RawMaterialInventoryEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public RawMaterialInventoryEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public  List<Object> findByRawSn(String sn) {
        List<Object> data = new ArrayList<>();
        var result = dao.findByRawSn(sn);
        for (RawMaterialInventoryEntity entity : result) {
            var whouse = warehouseDao.findBySN(entity.getWhSN());
            var s = entity.toMap();
            if(whouse!=null){
                s.put("stand",whouse.getStand());
            }else{
                s.put("stand","");
            }
            data.add(s);
        }
        return data;
    }

    @Transactional
    public Integer saveRawMaterialInventory(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        RawMaterialInventoryEntity entity = new RawMaterialInventoryEntity();
        if (Tl.isEmpty(sn)) {
            //执行保存
            Tl.getChangeLog(entity, model);
            var info=dao.findByWHSNAndRmSN(entity.getWhSN(), entity.getRmSN());
            if(info!=null){
                return -2;
            }
            //entity.setPYFieldValue();
            entity.setSn(queryNewSN(RawMaterialInventoryEntity.class));
            var rawInfo = rawMaterialInfoDao.findBySN(entity.getRmSN());
            entity.setRmCateName(rawInfo.getRmCateName());
            entity.setRmName(rawInfo.getRmName());
            entity.setRmSpec(rawInfo.getRmSpec());
            var wareHouse = warehouseDao.findBySN(entity.getWhSN());
            entity.setWhName(wareHouse.getWhName()+"["+rawInfo.getRmName()+"/"+rawInfo.getRmSpec()+"]");
            entity.setPlName(wareHouse.getPlName());
            entity.setMaxQuantity(wareHouse.getMaxQuantity());
            entity.setAlertQuantity(wareHouse.getAlertQuantity());
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入原材库存信息表信息", Tl.toJson(entity), RawMaterialInventoryEntity.class);
                var cangKu=warehouseDao.findBySN(entity.getWhSN());
                cangKu.setInventory(dao.getSumQuantityByWhSN(entity.getWhSN()));
                warehouseDao.update(cangKu);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            var rawInfo = rawMaterialInfoDao.findBySN(entity.getRmSN());
            entity.setRmCateName(rawInfo.getRmCateName());
            entity.setRmName(rawInfo.getRmName());
            entity.setRmSpec(rawInfo.getRmSpec());
            var wareHouse = warehouseDao.findBySN(entity.getWhSN());
            entity.setWhName(wareHouse.getWhName()+"["+rawInfo.getRmName()+"/"+rawInfo.getRmSpec()+"]");
            entity.setPlName(wareHouse.getPlName());
            entity.setMaxQuantity(wareHouse.getMaxQuantity());
            entity.setAlertQuantity(wareHouse.getAlertQuantity());
            //entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新原材库存信息表信息", Tl.toJson(entity), RawMaterialInventoryEntity.class);
                var cangKu=warehouseDao.findBySN(entity.getWhSN());
                cangKu.setInventory(dao.getSumQuantityByWhSN(entity.getWhSN()));
                warehouseDao.update(cangKu);
            }
        }

        return result;
    }

    @Transactional
    public Integer delete(String[] sn) {
        var result=dao.changeStateBySn(sn,(short)-1);
        if(result>0){
            for (var str: sn) {
                var entity=findBySn(str);
                writeLog(ActionType.DELETE, str, "删除原材库存信息表信息", Tl.toJson(entity), RawMaterialInventoryEntity.class);
            }
        }
        return result;
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        var result=dao.changeStateBySn(snArray,state);
        if(result>0){
            for (var str: snArray) {
                var entity=findBySn(str);
                writeLog(ActionType.DELETE, str, "更新原材库存信息表信息", Tl.toJson(entity), RawMaterialInventoryEntity.class);
            }
        }
        return result;
    }

    @Override
    public List<RawMaterialInventoryEntity> queryByWouSN(String sn) {
        return dao.getListByWhSN(sn);
    }
}